package fr.utbm.gi.in56.ads;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.utbm.gi.in56.database.DatabaseServlet;
import fr.utbm.gi.in56.error.ErrorType;
import fr.utbm.gi.in56.user.UserBean;
import fr.utbm.gi.in56.user.UserManager;

/**
 * Servlet implementation class MyAdsServlet
 * 
 * @author Adrien Hoffer & Tarik Belabbas
 */
@WebServlet("/MyAdsServlet")
public class MyAdsServlet extends DatabaseServlet
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyAdsServlet()
    {
        super();
     
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		request.setCharacterEncoding( "UTF-8" ); //$NON-NLS-1$

		forwardAds( request, response );
		
		RequestDispatcher rd = request.getRequestDispatcher( "myAds.jsp" ); //$NON-NLS-1$
		rd.forward( request, response );
	}
	
	protected void forwardAds( HttpServletRequest request, HttpServletResponse response )
	{
		// Get the current user
		HttpSession session = request.getSession( true );
		UserBean user = (UserBean)session.getAttribute( "currentSessionUser" ); //$NON-NLS-1$

		AdsManager adsMg = new AdsManager( getConnection() );
		UserManager userMg = new UserManager( getConnection() );
		
		List< AdBuy > adBuyList = adsMg.getBuyAds( 0, 0, 2, -1, 0, -1, 0,
				10, 0, false, -1, -1, 0, user.getLogin(), "" );
		request.setAttribute( "adBuyList", adBuyList ); //$NON-NLS-1$
		
		List< List< String > > usersAllowedBuy = new LinkedList< List<String> >();
		for ( AdBuy currentAd : adBuyList )
		{
			usersAllowedBuy.add( userMg.getUsersWhichSentAMessageAbout(
					user.getLogin(), AdType.BUY, currentAd.getId() ) );
			
		}
		request.setAttribute( "usersAllowedBuy", usersAllowedBuy ); //$NON-NLS-1$
		
		List< AdBuy > adOtherBuyList = adsMg.getBuyAds( 0, 0, 2, -1, 0, -1, 0,
				10, 0, false, -1, -1, 0, "", user.getLogin() );
		request.setAttribute( "adOtherBuyList", adOtherBuyList ); //$NON-NLS-1$
		
		List< AdSell > adSellList = adsMg.getSellAds( 0, 0, 2, -1, -1, 0, 0,
				-1, -1, 0, 10, 0, false, -1, -1, 0, "", user.getLogin() );
		request.setAttribute( "adSellList", adSellList ); //$NON-NLS-1$
		
		List< List< String > > usersAllowedSell = new LinkedList< List<String> >();
		for ( AdSell currentAd : adSellList )
		{
			usersAllowedSell.add( userMg.getUsersWhichSentAMessageAbout(
					user.getLogin(), AdType.SELL, currentAd.getId() ) );
			
		}
		request.setAttribute( "usersAllowedSell", usersAllowedSell ); //$NON-NLS-1$
		
		List< AdSell > adOtherSellList = adsMg.getSellAds( 0, 0, 2, -1, -1, 0, 0,
				-1, -1, 0, 10, 0, false, -1, -1, 0, user.getLogin(), "" );
		request.setAttribute( "adOtherSellList", adOtherSellList ); //$NON-NLS-1$
	}
}